/*This file is part of WZLibCP.

 *  WZLibCP is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

 *  WZLibCP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
    along with WZLibCP.  If not, see <http://www.gnu.org/licenses/>.
		
	Author: calc0000
*/
#ifndef __WZLIB_UTILS_H
#define __WZLIB_UTILS_H

/** \file WZLibUtils.h
\brief Contains various utility functions/classes/structs for WZLibCP.
*/

#pragma warning(disable:4996)
#include <string>
#include <cstring>
#include <cstdlib>
#include <vector>
#define ctor

/**\var typedef unsigned char byte
\brief Typedef for a byte.
*/
typedef unsigned char byte;
/**\var typedef signed char sbyte
\brief Typedef for a signed byte.
*/
typedef signed char sbyte;

namespace WZLib{
	/**
	\brief A static class to contain various configuration info.

	WZConfig has two members: imagePath and imageDir.  These members
	are only really useful when a program is run from the Visual Studio IDE.
	*/
	class WZConfig{
	public:
		//!The path of the image file (a .exe on Windows).
		static std::string	imagePath;
		//!The directory in which the image file resides.
		static std::string	imageDir;
		//!The directory to open WZ files from
		static std::string	wzSearchDir;
	};

	/**
	\brief Represents a point (not used in WZLib, but is used in MSLib).
	*/
	class WZPoint{
	public:
		//!x value of the point
		int x;
		//!y value of the point
		int y;
		//!Constructor.  Initializes x and y to 0.
		WZPoint(){x=y=0;}
	};
}

/**
\fn std::string getFileName(std::string path)
\brief Returns the last element of a given path.

For example, if given /home/joe/wzlib, the function will return wzlib.
\param path The path to be manipulated.
*/
std::string getFileName(std::string path);
/**
\fn std::string getDirName(std::string path)
\brief Truncates the last element of a given path.

For example, if given /home/joe/wzlib, the function will return /home/joe.
\param path The path to be manipulated.
*/
std::string getDirName(std::string path);
/**
\fn std::string lower(std::string s)
\brief Forces s to lowercase.

If given 'WZLibCP', it will return 'wzlibcp'.

Not used in WZLibCP.
\param s The string to be lowered.
*/
std::string lower(std::string s);
/**
\fn std::vector<std::string> tokenize(std::string str,std::string del,bool dropEmpty=true)
\brief Splits str based on the delimiters in del, dropping empty elements if dropEmpty
is true.

If given 'a|b|c|d' with '|' as the delimiters, it will return a vector
with the elements ['a','b','c','d'].  Used in WZUOLProperty to resolve
UOL's.

\param str The string to be split.
\param del The delimiters to use (will match any of the characters, not the whole string).
\param dropEmpty Whether or not to return elements that have no size, i.e. ''.

*/
std::vector<std::string>	tokenize(std::string str,std::string del,bool dropEmpty=true);
/**
\fn int z_decompress(unsigned char* inBuffer,int inLen,unsigned char** outBufferPtr,int* outLen)
\brief Uses zlib's inflate to decompress inBuffer of length inLen, and places the decompressed data into *outBufferPtr

It is the caller's responsibility to free inBuffer and *outBufferPtr when no longer needed.
Returns Z_OK or the error code reported by zlib.

Example:\code
unsigned char* outBuffer=NULL;
int outLen;
int err=z_decompress(inBuffer,inLen,&outBuffer,&outLen);
if(err!=Z_OK)
	printf("There was an error: %d\n",err);
\endcode

\param inBuffer	The buffer to be decompressed.
\param inLen	The length of inBuffer (in bytes).
\param outBufferPtr	A pointer to a buffer to receive the output.
\param outLen		A pointer to an integer to receive the length of *outBufferPtr
*/
int z_decompress(unsigned char* inBuffer,int inLen,unsigned char** outBufferPtr,int* outLen);
/**
\fn int z_compress(unsigned char* inBuffer,int inLen,int level,unsigned char** outBufferPtr,int* outLen)
\brief Uses zlib's deflate to compress inBuffer of length inLen to level, and places the compressed data into *outBufferPtr

It is the caller's responsibility to free inBuffer and *outBufferPtr when no longer needed.
Returns Z_OK or the error code reported by zlib.

Example:\code
unsigned char* outBuffer=NULL;
int outLen;
int err=z_compress(inBuffer,inLen,6,&outBuffer,&outLen);
if(err!=Z_OK)
	printf("There was an error: %d\n",err);
\endcode

\param inBuffer	The buffer to be compressed.
\param inLen	The length of inBuffer (in bytes).
\param level	The level to compress to (0-9)
\param outBufferPtr	A pointer to a buffer to receive the output.
\param outLen		A pointer to an integer to receive the length of *outBufferPtr
*/
int z_compress(unsigned char* inBuffer,int inLen,int level,unsigned char** outBufferPtr,int* outLen);
#endif
